--
-- Author: shunguo.chen
-- Date: 2018-03-09 10:52:18
--
-- ChessInfo 星空谜棋信息

local ChessPlayer = import(".ChessPlayer")
local ChessCell = import(".ChessCell")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	if data then
		self:init(data)
	end

end

-- //星空迷棋数据
-- message S2C_StarChessGameInfo{
-- 	repeated StarChessCellBaseInfo cells = 1;//棋盘信息，只会存放有事件的格子
-- 	StarChessPlayer self = 2;//自己
-- 	base.UserBaseInfo selfUserInfo =3;//自己的基本信息，外显，等级，名字等，与游戏无关，只会在S2C_StarChessGameInfo中发送
-- 	StarChessPlayer target = 4;//对方
-- 	base.UserBaseInfo targetUserInfo = 5;//对方的基本信息，其他同上
-- 	int32 roundID = 6;//回合数，从1开始
-- 	int64 roundUID = 7;//拥有当前回合的UID
-- 	STARCHESS_ACTIVESTATUS status = 8;//当前状态
-- 	int64 actionUID = 9;//正在执行动作的UID
-- 	int32 remainTime = 10;//当前状态的剩余时间
-- }
function ClassRef:init(data)
	local cells = {}
	for i,v in ipairs(data.cells) do
		cells[v.pos] = ChessCell.new(v)
	end
	self.cells = cells

	self.myPlayer = ChessPlayer.new(data.self)
	self.myPlayer:setBaseInfo(data.selfUserInfo)
	self.enemyPlayer = ChessPlayer.new(data.target)
	self.enemyPlayer:setBaseInfo(data.targetUserInfo)

	self.roundID = data.roundID
	self.roundUID = data.roundUID
	self.status = data.status
	self.actionUID = data.actionUID
	self.remainTime = data.remainTime

	self._initTime = app:getTime()
end

function ClassRef:setInitTime(time)
	self._initTime = time
end

-- 剩余时间
function ClassRef:getRemainTime()
	local curTime = app:getTime()
	return self.remainTime - math.ceil(curTime - self._initTime)
end

-- 更新cell，是否被触发过
function ClassRef:updateCell(pos, uid)
	local cell = self.cells[pos]
	if cell then
		cell:addTrigger(uid)
	end
end

function ClassRef:updatePlayers(players)
	for i,player in ipairs(players) do
		if player.baseInfo.uid == self.myPlayer.uid then
			self.myPlayer:init(player)
		else
			self.enemyPlayer:init(player)
		end
	end
end

function ClassRef:getBaseInfos()
	return self.myPlayer.baseInfo, self.enemyPlayer.baseInfo
end

function ClassRef:setChessSelect(select)
	self._chessSelect = select
end

function ClassRef:getChessSelect()
	return self._chessSelect
end

function ClassRef:getVSInfos()
	local infos = {
		{self.myPlayer.baseInfo},
		{self.enemyPlayer.baseInfo},
		{self.myPlayer.baseInfo.name, self.enemyPlayer.baseInfo.name}
	}
	return infos
end

function ClassRef:isMe(uid)
	return self.myPlayer.uid == uid
end

function ClassRef:isNormalRollAble()
	return self:isMyRound() and self:isDice() and self.myPlayer.normalDiceNum > 0
end

function ClassRef:isGoldRollAble()
	return self:isMyRound() and self:isDice() and self.myPlayer.goldeDiceNUm > 0
end

-- 是否开始
function ClassRef:isStart()
	return self.roundID > 0
end

-- 是战斗中
function ClassRef:isBattle()
	return self.status == "BATTLE"
end

-- 是掷骰子
function ClassRef:isDice()
	return self.status == "DICE"
end

-- 是移动
function ClassRef:isMove()
	return self.status == "MOVE"
end

function ClassRef:isMyRound()
	return self.roundUID == self.myPlayer.uid
end

function ClassRef:getRoundMsg()
	if self:isMyRound() then
		if self.roundID == 1 then
			return L("starChessFirstStart")
		else
			return L("starChessYourTurn")
		end
	else
		if self.roundID == 1 then
			return L("starChessSecondStart")
		else
			return L("starChessNotYourTurn")
		end
	end
end

-- 获取当前回合玩家
function ClassRef:getCurPlayer()
	if self:isMyRound() then
		return self.myPlayer
	else
		return self.enemyPlayer
	end
end

function ClassRef:getNextPlayer()
	if self:isMyRound() then
		return self.enemyPlayer
	else
		return self.myPlayer
	end
end

function ClassRef:isMyAction()
	return self.actionUID == self.myPlayer.uid
end

-- 机器人 uid < 0
function ClassRef:isRobotAction()
	return self.actionUID < 0
end

-- 获取当前行动玩家
function ClassRef:getCurActionPlayer()
	if self:isMyAction() then
		return self.myPlayer
	else
		return self.enemyPlayer
	end
end

-- 获取当前行动玩家
function ClassRef:getCurNoActionPlayer()
	if self:isMyAction() then
		return self.enemyPlayer
	else
		return self.myPlayer
	end
end

function ClassRef:getPlayer(uid)
	if self.myPlayer.uid == uid then
		return self.myPlayer
	else
		return self.enemyPlayer
	end
end

-- 需要计时
function ClassRef:needCountTime()
	return self.status == "DICE" or self.status == "SELECT"
end

-- 计时的文本
function ClassRef:getCountTimeStr()
	if self.status == "DICE" then
		return L("lua_code_text_301")
	elseif self.status == "SELECT" then
		return L("lua_code_text_304")
	end
end

function ClassRef:getStatusStr()
	if self.status == "DICE" then
		return L("lua_code_text_96")
	elseif self.status == "MOVE" then
		return L("lua_code_text_130")
	elseif self.status == "SELECT" then
		return L("lua_code_text_383")
	end
end

return ClassRef